

-- Namespace
	local _,mod = ...


-- Constants
	local smfont = {mod.path..'media\\bavaria.ttf',8,'OUTLINEMONOCHROME'}
	local lgfont = {mod.path..'media\\bavaria.ttf',16,'OUTLINEMONOCHROME'}
	
	local white = { r = 1, g = 1, b = 1 }


-- Region creation
	mod.regions['alerts'] = function()
		local base = mod:CreateFrame('Frame',nil,UIParent)
			base:SetFrameStrata('MEDIUM')
		
		local text = base:CreateFontString(nil,'OVERLAY')
			text:SetFont(unpack(smfont))
			text:SetJustifyH('RIGHT')
			base.text = text
		
		local function fadeout(elapsed)
			local self = base
			local min = self.min
			min = min + elapsed; self.min = min
			if min >= 2 then
				self:Finished()
			elseif min >= 1 then
				self:SetAlpha(1-((min-1)/1))
			end
		end
		
		local function countdown(elapsed)
			local self = base
			local min = self.min
			min = min + elapsed; self.min = min
			local time = self.time
			if min >= time then
				self.text:SetText(self.msg:format(0))
				self.min = 0
				mod:timer_unregister(countdown)
				mod:anim_register(fadeout)(0)
			else
				self.text:SetText(self.msg:format(time-min))
			end
		end
		
		base:Listen('message',function(self,text,crit,r,g,b,time)
			self.text:SetFont(unpack(crit and lgfont or smfont))
			if r then
				self.text:SetTextColor(r,g,b)
			end
			self.min = 0
			if time then
				self.msg = text
				self.time = time
				mod:timer_register(.2,true,countdown)(0)
			else
				self.text:SetText(text)
				mod:anim_register(fadeout)(0)
			end
		end)
		
		base:Listen('reset',function(self)
			self:SetAlpha(1)
			self.text:SetText('')
			self.text:SetTextColor(1,1,1)
			self.msg = nil
			self.time = nil
			self.min = nil
			mod:anim_unregister(fadeout)
		end)
		
		base:Listen('order',function(self,id)
			self.text:ClearAllPoints()
			self.text:SetPoint('CENTER',UIParent,'CENTER',0,32-((id-1)*16))
		end)
		
		return base
	end


-- Events: procs
	local triggers = {
		--[spid] = {crit,   r,  g,  b,time},
		[110909] = {true, 242,245,196,   6},  -- Alter Time
		[034936] = {true, 190, 95,190, nil},  -- Backlash
		[093622] = {true, 214, 73, 73, nil},  -- Berserk
		[046916] = {true, 214, 73, 73, nil},  -- Bloodsurge
		[044549] = {true,  96,183,230, nil},  -- Brain Freeze
		[057761] = {true,  96,183,230, nil},  -- Brain Freeze
		[086949] = {true, 235,124, 68, nil},  -- Cauterize
		[016870] = {true, 255,255,255, nil},  -- Clearcasting
		[108869] = {true, 214,108, 73, nil},  -- Decimation
		[121153] = {true, 214, 73, 73, nil},  -- Dispatch
		[048517] = {true, 230,183, 96, nil},  -- Eclipse (Solar)
		[048518] = {true,  96,183,230, nil},  -- Eclipse (Lunar)
		[128939] = {true, 230,228, 96, nil},  -- Elusive Brew
		[005308] = {true, 237,154,126, nil},  -- Execute
		[044544] = {true, 117,190,230, nil},  -- Fingers of Frost
		[059052] = {true, 117,190,230, nil},  -- Freezing Fog
		[024275] = {true, 237,154,126, nil},  -- Hammer of Wrath
		[053351] = {true, 237,154,126, nil},  -- Kill Shot
		[051124] = {true, 230,117,175, nil},  -- Killing Machine
		[077762] = {true, 235,124, 68, nil},  -- Lava Surge
		[056453] = {true, 214, 73, 73, nil},  -- Lock and Load
		[053817] = {true, 230,117,175, nil},  -- Maelstrom Weapon
		[115867] = {true, 117,230,228, nil},  -- Mana Tea
		[122351] = {true, 235,114, 87, nil},  -- Molten Core
		[108558] = {true, 190, 95,190, nil},  -- Nightfall
		[048391] = {true, 117,230,228, nil},  -- Owlkin Frenzy
		[118636] = {true, 219,235,134, nil},  -- Power Guard
		[069369] = {true, 219,235,134, nil},  -- Predator's Swiftness
		[006572] = {true, 235,144,134, nil},  -- Revenge
		[059057] = {true, 134,145,235, nil},  -- Rime
		[056815] = {true, 139,134,235, nil},  -- Rune Strike
		[007384] = {true, 200,200,200, nil},  -- Overpower
		[017941] = {true, 180,100,220, nil},  -- Shadow Trance
		[114842] = {true, 240,200,220,   6},  -- Shadow Walk
		[023922] = {true, 232,218,128, nil},  -- Shield Slam
		[091342] = {true,  90,190, 80, nil},  -- Shadow Infusion
		[077487] = {true, 123, 80,190, nil},  -- Shadow Orb
		[093400] = {true, 123, 80,190, nil},  -- Shooting Stars
		[108208] = {true, 242,245,196,   3},  -- Subterfuge?
		[115192] = {true, 242,245,196,   3},  -- Subterfuge
		[052437] = {true, 190,180, 80, nil},  -- Sudden Death
		[081340] = {true, 160,145,200, nil},  -- Sudden Doom
		[050227] = {true, 190,200,145, nil},  -- Sword and Board
		[056638] = {true, 185, 90, 90, nil},  -- Taste for Blood
		[115610] = {true, 200,240,240,   4},  -- Temporal Shield
		[059578] = {true, 240,240,200, nil},  -- The Art of War
		[053390] = {true, 117,190,230, nil},  -- Tidal Waves
		[034428] = {true, 240,200,230, nil},  -- Victory Rush
		[122107] = {true, 117,230,228, nil},  -- Vital Mists
	}
	for sp,data in pairs(triggers) do
		data[2] = data[2] / 255
		data[3] = data[3] / 255
		data[4] = data[4] / 255
	end
	
	local player = mod:unit('player')
	
	local function CLEU(_,ev,_,_,_,_,_,dest,_,_,_,...)
		if not player(dest) then return end
		
		if ev ~= 'SPELL_AURA_APPLIED' and ev ~= 'SPELL_AURA_REFRESH' and ev ~= 'SPELL_AURA_APPLIED_DOSE' then
			return
		end
		
		local spid,spname = ...
		local t = triggers[spid]
		if not t then return end
		
		mod:message('alerts',
			(t[5] and spname..': %d!' or spname..'!'),
			unpack(t)
		)
	end
	
	mod:event_register('COMBAT_LOG_EVENT_UNFILTERED',CLEU)


-- Events: +/- combat
	mod:event_register('PLAYER_REGEN_ENABLED',function()
		mod:message('alerts',
			'-Combat!'
		)
	end)
	
	mod:event_register('PLAYER_REGEN_DISABLED',function()
		mod:message('alerts',
			'+Combat!'
		)
	end)


-- Events: skill/reputation
	mod:event_register('CHAT_MSG_SKILL',function(msg)
		local prof,to = (msg or ''):match('^Your skill in (.*) has increased to (%d+).')
			to = tonumber(to)
		if prof and to then
			mod:message('alerts',
				('+%s: %d'):format(prof,to),
				nil, .6,.6,1
			)
		end
	end)
	
	mod:event_register('CHAT_MSG_COMBAT_FACTION_CHANGE',function(msg)
		local with,type,by = (msg or ''):format('^Reputation with (.*) ([inde]+)creased by (%d+).')
			by = tonumber(by)
		if with and type and by then
			mod:message('alerts',
				('%s: %s%d'):format(with,(type == 'in' and '+' or '|cffff7f7f-|r'),by),
				nil, .6,1,.6
			)
		end
	end)
